package cc.tuhaolicai.mt.user.mobile.controller;

import cc.tuhaolicai.mt.user.mobile.annotation.Mapping;
import com.jfinal.aop.Clear;
import com.jfinal.ext.interceptor.syslog.SysLogInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroInterceptor;
import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pro.tools.constant.StrConst;
import pro.tools.data.text.ToolRandoms;
import pro.tools.data.text.ToolStr;
import pro.tools.file.ToolFile;
import pro.tools.system.ToolOS;
import pro.tools.time.DatePlus;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 用于公用工具方法
 *
 * @author SeanDragon
 *         <p>
 *         Create By 2017-07-03 11:57
 */
@Mapping("/tool")
@Clear({ShiroInterceptor.class, SysLogInterceptor.class})
public class ToolController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(ToolController.class);

    public void uploadFile() {

        Map<String, String> result = new HashMap<>();
        String statusCode;

        UploadFile file = getFile();
        String dir = getPara("dir");
        if (ToolStr.isBlank(dir)) {
            dir = "";
        }

        DatePlus today = new DatePlus();
        String dateDir = today.toString("yyyy;MM;dd".replace(";", StrConst.FILE_SEP));//加上日期分类

        String destFile = ToolOS.USER_HOME + StrConst.FILE_SEP + "pictures" + StrConst.FILE_SEP + dir + StrConst.FILE_SEP + dateDir + StrConst.FILE_SEP + ToolRandoms.getRandomStr() + "." + ToolFile.getFileExtension(file.getFile());

        InputStream inputStream = null;
        OutputStream outputStream = null;

        byte[] data;
        try {
            ToolFile.createOrExistsFile(destFile);

            inputStream = new FileInputStream(file.getFile());
            outputStream = new FileOutputStream(destFile);

            int i = inputStream.available();
            data = new byte[i];
            inputStream.read(data);
            outputStream.write(data);

            statusCode = "200";
        } catch (IOException e) {
            e.printStackTrace();
            statusCode = "300";
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        result.put("statusCode", statusCode);
        result.put("filename", destFile);
        renderJson(result);
    }

    public void readFile() {
        String picPath = getPara("path");

        if (!ToolFile.isFileExists(picPath)) {
            picPath = PathKit.getWebRootPath() + StrConst.FILE_SEP + "image" + StrConst.FILE_SEP + "index" + StrConst.FILE_SEP + "footerLogo.jpg";
        }

        byte[] data;
        try (FileInputStream in = new FileInputStream(picPath)) {
            int i = in.available();
            data = new byte[i];
            in.read(data);
            getResponse().getOutputStream().write(data);
            renderNull();
        } catch (IOException e) {
            log.info("图片丢失", e);
            renderText("您访问的图片丢失了");
        }

    }
}
